<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	// Make sure this file isn't being accessed directly
	if(!defined('IN_X7CHAT3')) return;
	
	$gmtnow = gmdate("U");
	$localnow = time();
	
	$display_year = lib_input::read('year',date("Y",$localnow),lib_input::NUMBER);
	if(empty($display_year) || $display_year < 1970 || $display_year > 2037)
		$display_year = date("Y",$localnow);
		
	
	$display_month = lib_input::read('month',date('n',$localnow),lib_input::NUMBER);
	if(empty($display_month) || $display_month < 1 || $display_month > 12){
		if($display_month > 12 && $display_year < 2037){
			$display_month = 1;
			$display_year++;
		}elseif($display_month < 1 && $display_year > 1970){
			$display_month = 12;
			$display_year--;
		}else
			$display_month = date("n",$localnow);
	}
	
	$start_of_month = gmmktime(0,0,0,$display_month,1,$display_year);
		
	list($last_day_of_month,$first_day_of_month) = explode(";",gmdate('t;w',$start_of_month));
	$todays_date = gmdate('d',$localnow);
	$this_month = gmdate('n',$localnow);
	
	$data = $GLOBALS['DATABASE']->get('system','events');
	$data = $data->get_next();
	$event_keys = array_keys($data);
	sort($event_keys);
	$next_event = -1;
	$cur_event = 0;
	
	$days = array();
	
	for($i = 1; $i <= 42; $i++){
		$loopdate = $i-$first_day_of_month;
		
		if($i <= $first_day_of_month){
			// Month has not started yet
			$days[$i] = array('label'=>'','events'=>'','today'=>false);
		}elseif($loopdate > $last_day_of_month){
			// Month has finished
			$days[$i] = array('label'=>'','events'=>'','today'=>false);
		}else{
			// Day of month
			$todays_events = '';
			$start_of_day = gmmktime(0,0,0,$display_month,$loopdate,$display_year);
			$end_of_day = $start_of_day+86400;
			
			while(isset($event_keys[$cur_event]) && $event_keys[$cur_event] < $end_of_day){
				if($next_event == -1 && $event_keys[$cur_event] > $gmtnow)
					$next_event = $cur_event;
				if($event_keys[$cur_event] > $start_of_day)
					$todays_events .= "<br /><b>".date($settings['long_date_format'],$data[$event_keys[$cur_event]]['time'])."</b><br />".$data[$event_keys[$cur_event]]['description']."<br /><br /><hr />";
					
				$cur_event++;
			}
			
			$days[$i] = array('label'=>$loopdate,'events'=>$todays_events,'today'=>($display_month == $this_month && $loopdate == $todays_date));
		}
	}
	
	if($next_event == -1 && count($event_keys) > 0)
		$next_event = 0;
	
	if($next_event == -1 || $event_keys[$next_event]['time'] < $gmtnow)
		$next_event = $GLOBALS['LANG']->lang_intro->no_events;
	else
		$next_event = "<b>".date($settings['long_date_format'],$data[$event_keys[$next_event]]['time'])."</b><br />".$data[$event_keys[$next_event]]['description']."<br />";
	
	$format = lib_input::read('format','outer');
		
	$GLOBALS['OUTPUT']->page_id = 'calendar';
	$GLOBALS['OUTPUT']->next_event = $next_event;
	$GLOBALS['OUTPUT']->caldata = $days;
	$GLOBALS['OUTPUT']->format = $format;
	$GLOBALS['OUTPUT']->month_name = $GLOBALS['LANG']->lang_intro->{'month_'.$display_month};
	$GLOBALS['OUTPUT']->month = $display_month;
	$GLOBALS['OUTPUT']->year = $display_year;
	
	if($format == 'inner')
		echo tpl_calendar::calendar_page_inner();
	else
		echo tpl_calendar::calendar_page_welcome();
		
	return;
?>